package com.bugreport.configuration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.unitils.UnitilsJUnit4;
import org.unitils.spring.annotation.SpringApplicationContext;
import org.unitils.spring.annotation.SpringBeanByType;

import com.bugreport.configuration.BugTrackerConfiguration;
import com.bugreport.configuration.BugTrackerConfigurationLoader;
import com.bugreport.test.utils.BugTrackerTestConfiguration1;
import com.bugreport.test.utils.BugTrackerTestConfiguration2;
import com.bugreport.utils.BugReportException;

@SpringApplicationContext("com/bugreport/configuration/BTCLLGetMatchingUrlConfigurationTestCase-spring.xml")
public class BTCLGetMatchingUrlConfigurationTestCase extends UnitilsJUnit4 {

	private static final Logger logger = Logger.getLogger(BTCLGetMatchingUrlConfigurationTestCase.class);

	@SpringBeanByType
	private BugTrackerConfigurationLoader loader;

	@Test
	public void matchFirstConfigurationWithExactUrl() throws BugReportException {

		// Given
		String url = "http://www.foo.bar";

		// When
		BugTrackerConfiguration configuration = loader.getMatchingConfiguration(url);

		// Then
		assertNotNull(configuration);
		assertEquals(new Integer(1), configuration.getIdConfig());
		assertTrue(configuration instanceof BugTrackerTestConfiguration1);
	}

	@Test
	public void matchSecondConfigurationWithLessRestrictiveUrl() throws BugReportException {

		// Given
		String url = "www.foo.bar";

		// When
		BugTrackerConfiguration configuration = loader.getMatchingConfiguration(url);

		// Then
		assertNotNull(configuration);
		assertEquals(new Integer(2), configuration.getIdConfig());
		assertTrue(configuration instanceof BugTrackerTestConfiguration2);
	}

	@Test
	public void matchSecondConfigurationWithCompleteUrl() throws BugReportException {

		// Given
		String url = "www.foo.bar/foo/bar.html";

		// When
		BugTrackerConfiguration configuration = loader.getMatchingConfiguration(url);

		// Then
		assertNotNull(configuration);
		assertEquals(new Integer(2), configuration.getIdConfig());
		assertTrue(configuration instanceof BugTrackerTestConfiguration2);
	}

	@Test
	public void matchThirdConfigurationWithSecuredCompleteUrl() throws BugReportException {

		// Given
		String url = "https://www.foo.bar/foo/bar.html";

		// When
		BugTrackerConfiguration configuration = loader.getMatchingConfiguration(url);

		// Then
		assertNotNull(configuration);
		assertEquals(new Integer(3), configuration.getIdConfig());
		assertTrue(configuration instanceof BugTrackerTestConfiguration1);
	}

	@Test
	public void doesNotMatchConfiguration() {

		// Given
		String url = "http://www.bar.foo/foo/bar.html";

		try {

			// When
			loader.getMatchingConfiguration(url);
			fail("Expected exception.");

		} catch (BugReportException ex) {

			assertNotNull(ex);
			assertEquals(103, ex.getCode());

			logger.debug(ex.getMessage());
		}
	}
}
